<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ProjectManage
 *
 * @author gensen
 */
class ProjectManage extends CI_Controller{
    //put your code here
    function __construct() {
        parent::__construct();
        if(date_default_timezone_get() != "1Asia/Shanghai") date_default_timezone_set("Asia/Shanghai");
        //$this->load->library('SalaryInfoZJ');
    }
    
    function index(){
        $arr_data = array();
        $projecttitle_data = $this->Data_model->get_data(array("ISDEL" => "0"),"","ID asc",-1,-1,"WEIXIN_PROJECTTITLE");
        foreach ($projecttitle_data as $value) {
            $project_data = $this->Data_model->get_data(array('FID' => $value['ID'],"ISDEL" => "0"),"","",-1,-1,"WEIXIN_PROJECT");
            $content = array("content" => $project_data);
            $new = array_merge($value,$content);
            array_push($arr_data, $new);
        }
        $res_data = array(
            "data" => $arr_data,
            "username" => ""
        );
        //var_dump($res_data['data'][0]);
        $this->load->view("ProjectManage",$res_data);
    }
    function indexByUserName(){
        $username = $_REQUEST['username'];
        $arr_data = array();
        $projecttitle_data = $this->Data_model->get_data(array("ISDEL" => "0"),"","ID asc",-1,-1,"WEIXIN_PROJECTTITLE");
        foreach ($projecttitle_data as $value) {
            $project_data = $this->Data_model->get_data(array('FID' => $value['ID'],"ISDEL" => "0"),"","",-1,-1,"WEIXIN_PROJECT");
            $content = array("content" => $project_data);
            $new = array_merge($value,$content);
            array_push($arr_data, $new);
        }
        $res_data = array(
            "data" => $arr_data,
            "username" => $username
        );
        //var_dump($res_data['data'][0]);
        $this->load->view("ProjectManage",$res_data);
    }
    function ProjectProcess(){
        $id = $_REQUEST['id'];
        $username = $_REQUEST['username'];
        $project = $this->Data_model->get_single_data("ID = ".$id,"WEIXIN_PROJECT");
        $process = $this->Data_model->get_data(array('FID' => $id),"","ID desc",-1,-1,"WEIXIN_PROJECTPROCESS");
        $process = array("process" => $process);
        $data = array_merge($project,$process);
        $res = array("data" => $data,"username" => $username);
        $this->load->view("ProjectProcess",$res);
    }
    
    //创建新的课题进程，并更新原有课题状态
    function UpdateProcess(){
        $id = $_REQUEST['id']; //process 的id
        $title = $_REQUEST['title'];
        $content = $_REQUEST['content'];
        $rate = $_REQUEST['rate'];
        $creater = $_REQUEST['creater'];
        $nowtime = date('y-m-d H:i:s');
        $this->Data_model->add_data_Oracle(
                array('FID' => $id,
                    'TITLE' => $title,
                    'CONTENT' => $content,
                    'CREATER' => $creater,
                    'CREATETIME' => $nowtime
            ),'WEIXIN_PROJECTPROCESS');
        if($rate == 100){
            $status = 2;
        }else{
            $status = 1; 
        }
        $this->Data_model->update_data(array("ID" => $id),array("UPDATETIME" => $nowtime,"STATUS" => $status,"LASTUPDATER" => $creater,"RATE" => $rate),"WEIXIN_PROJECT");
        $data = array("status" => 1);
        echo json_encode($data);
    }
    function admin(){
        $this->load->view("ProjectAdmin");
    }
    function projecttitle(){
        $data = $this->Data_model->get_data(array("ISDEL" => "0"),"","ID asc",-1,-1,"WEIXIN_PROJECTTITLE");
        $i = 1;
        $newdata = array();
        foreach ($data as $value) {
            $value['kid'] = $i;
            $i++;
            array_push($newdata, $value);
        }
        $data = array('data' => $newdata,'type' => 'title');
        $this->load->view("ProjectTitle",$data);
    }
    function createProjectTitle(){
        $proname = $_REQUEST['proname'];
        $nowtime = date('y-m-d H:i:s');
        $this->Data_model->add_data_Oracle(array("PROJECTNAME" => $proname,"CREATETIME" => $nowtime),"WEIXIN_PROJECTTITLE");
        $data = array("status" => 1);
        echo json_encode($data);
    }
    function updateProjectTitle(){
        $proname = $_REQUEST['proname'];
        $id = $_REQUEST['id'];
        $this->Data_model->update_data(array("ID" => $id),array("PROJECTNAME" => $proname),"WEIXIN_PROJECTTITLE");
        $data = array("status" => 1);
        echo json_encode($data);
    }
    function delProjectTitle(){
        $id = $_REQUEST['id'];
        $this->Data_model->update_data(array("ID" => $id),array("ISDEL" => 1),"WEIXIN_PROJECTTITLE");
        $data = array("status" => 1);
        echo json_encode($data);
    }
    function project(){
        $data = $this->Data_model->get_data(array("ISDEL" => "0"),"","ID asc",-1,-1,"WEIXIN_PROJECT");
        $pdata = $this->Data_model->get_data(array("ISDEL" => "0"),"","ID asc",-1,-1,"WEIXIN_PROJECTTITLE");
        $i = 1;
        $newdata = array();
        foreach($data as $value){
            $title = $this->Data_model->get_single_data("ID = ".$value['FID'],'WEIXIN_PROJECTTITLE');
            $value['PROJECTNAME'] = $title['PROJECTNAME'];
            $value["kid"] = $i;
            $i++;
            array_push($newdata, $value);
        }
        //var_dump($newdata);
        $data = array('data' => $newdata,'type' => 'content','pdata' => $pdata);
        $this->load->view("ProjectTitle",$data);
    }
    function createProject(){
        $pid = $_REQUEST['pid'];
        $title = $_REQUEST['title'];
        $content = $_REQUEST['content'];
        $income = $_REQUEST['income'];
        $rate = $_REQUEST['rate'];
        $creater = $_REQUEST['creater'];
        $nowtime = date('y-m-d H:i:s');
        $this->Data_model->add_data_Oracle(
                array("FID" => $pid,
                    "TITLE" => $title,
                    "CONTENT" => $content,
                    "INCOME" => $income,
                    "RATE" => $rate,
                    "CREATER" => $creater,
                    "CREATETIME" => $nowtime),"WEIXIN_PROJECT");
        $data = array("status" => 1);
        echo json_encode($data);
    }
    function updateProject(){
        $id = $_REQUEST['id'];
        $title = $_REQUEST['title'];
        $content = $_REQUEST['content'];
        $income = $_REQUEST['income'];
        $creater = $_REQUEST['creater'];
        $nowtime = date('y-m-d H:i:s');
        $this->Data_model->update_data(array("ID" => $id),array("TITLE" => $title,"CONTENT" => $content,"INCOME" => $income,"CREATER" => $creater,"UPDATETIME" => $nowtime),"WEIXIN_PROJECT");
        $data = array("status" => 1);
        echo json_encode($data);
    }
    function delProject(){
        $id = $_REQUEST['id'];
        $this->Data_model->update_data(array("ID" => $id),array("ISDEL" => 1),"WEIXIN_PROJECT");
        $data = array("status" => 1);
        echo json_encode($data);
    }
}
